#Title: combinacion de numeros		Filename: ej60.s
# Autho: Arturo Luquin Castillo           Date 04/05/2017
# Description: combinacion de 4 numeros de 1-15 no repetibles 
# Input: -
# Output: si X^3+y^3=w^3+Z^3 imprime valores


@@@@@@@@@@@
@Registros@
@@@@@@@@@@@

@r1 apunta a la direccion de primo
@r3 el numero actual
@r4 contador numeros
@r9 numeros vistos

.section .bss
.comm nums, 400       @ reserva espacio para numeros 4bits por numero

.section  .data

spc:                    @separarlos con +
        .ascii" + "
len= . - spc

nl:
      .ascii "\n"       @renglon de separacion
      
limit:               @indice del ultimo numero que necesitaremos.
      .long 100
 
 
 .section .text
.global main /* 'main' is our entry point and must be global */

main:
	ldr 	r8, =nums
	ldr 	r7, =limit
	ldr 	r9, [r7]
        str     fp, [sp, #-4]!
        add     fp, sp, #0
        sub     sp, sp, #36
        mov     r3, #0
        str     r3, [fp, #-8]
        mov     r3, #0
        str     r3, [fp, #-12]
        mov     r3, #0
        str     r3, [fp, #-16]
        mov     r3, #0
        str     r3, [fp, #-20]
        mov     r3, #0
        str     r3, [fp, #-28]
        mov     r3, #0
        str     r3, [fp, #-32]
        mov     r3, #0
        str     r3, [fp, #-24]
        mov     r3, #1
        str     r3, [fp, #-8]
        b       .L2
.L10:
        mov     r3, #1
        str     r3, [fp, #-12]
        b       .L3
.L9:
        mov     r3, #1
        str     r3, [fp, #-16]
        b       .L4
.L8:
        mov     r3, #1
        str     r3, [fp, #-20]
        b       .L5
.L7:
        ldr     r2, [fp, #-8]
        ldr     r3, [fp, #-12]
        cmp     r2, r3
        beq     .L6
        ldr     r2, [fp, #-8]
        ldr     r3, [fp, #-16]
        cmp     r2, r3
        beq     .L6
        ldr     r2, [fp, #-8]
        ldr     r3, [fp, #-20]
        cmp     r2, r3
        beq     .L6
        ldr     r2, [fp, #-12]
        ldr     r3, [fp, #-16]
        cmp     r2, r3
        beq     .L6
        ldr     r2, [fp, #-12]
        ldr     r3, [fp, #-20]
        cmp     r2, r3
        beq     .L6
        ldr     r2, [fp, #-16]
        ldr     r3, [fp, #-20]
        cmp     r2, r3
        beq     .L6
        ldr     r3, [fp, #-12]
        ldr     r2, [fp, #-12]
        mul     r3, r2, r3
        ldr     r2, [fp, #-12]
        mul     r3, r2, r3
        str     r3, [fp, #-32]
        ldr     r3, [fp, #-8]
        ldr     r2, [fp, #-8]
        mul     r3, r2, r3
        ldr     r2, [fp, #-8]
        mul     r3, r2, r3
        str     r3, [fp, #-32]
        ldr     r3, [fp, #-16]
        ldr     r2, [fp, #-16]
        mul     r3, r2, r3
        ldr     r2, [fp, #-16]
        mul     r3, r2, r3
        str     r3, [fp, #-28]
        ldr     r3, [fp, #-20]
        ldr     r2, [fp, #-20]
        mul     r3, r2, r3
        ldr     r2, [fp, #-20]
        mul     r3, r2, r3
        str     r3, [fp, #-28]
        ldr     r2, [fp, #-28]
        ldr     r3, [fp, #-32]
        cmp     r2, r3
        bne     .L6
        ldr     r3, [fp, #-12]
        str     r3, [r8, #4]!
        ldr     r3, [fp, #-8]
        str     r3, [r8, #4]!
        ldr     r3, [fp, #-16]
        str     r3, [r8, #4]!
        ldr     r3, [fp, #-20]
        str     r3, [r8, #4]!
.L6:
        ldr     r3, [fp, #-20]
        add     r3, r3, #1
        str     r3, [fp, #-20]
.L5:
        ldr     r3, [fp, #-20]
        cmp     r3, #15
        movgt   r3, #0
        movle   r3, #1
        and     r3, r3, #255
        cmp     r3, #0
        bne     .L7
        ldr     r3, [fp, #-16]
        add     r3, r3, #1
        str     r3, [fp, #-16]
.L4:
        ldr     r3, [fp, #-16]
        cmp     r3, #15
        movgt   r3, #0
        movle   r3, #1
        and     r3, r3, #255
        cmp     r3, #0
        bne     .L8
        ldr     r3, [fp, #-12]
        add     r3, r3, #1
        str     r3, [fp, #-12]
.L3:
        ldr     r3, [fp, #-12]
        cmp     r3, #15
        movgt   r3, #0
        movle   r3, #1
        and     r3, r3, #255
        cmp     r3, #0
        bne     .L9
        ldr     r3, [fp, #-8]
        add     r3, r3, #1
        str     r3, [fp, #-8]
.L2:
        ldr     r3, [fp, #-8]
        cmp     r3, #15
        bge     P10
        movgt   r3, #0
        movle   r3, #1
        and     r3, r3, #255
        cmp     r3, #0
        bne     .L10
        mov     r3, #0
        mov     r0, r3
        add     sp, fp, #0
        ldmfd   sp!, {fp}
        bx      lr
        
P10:
 mov r0,$1
 mov r4,$0
 mov r5,$0
 ldr r6,=nums
 ldr r3,[r6]

printLoop:
bl print_num		@ function call
add r4, $1		@ add one to temp counter
add r5, $1		@ add one to counter
cmp r5, r9 		@ are we done?
bge exit		@ if so, exit
cmp r4, $9		@ after 10 primes ...
bgt newline		@ print a newline
ble space		@ add spaces

space:                  @ we jump here if
mov r0, $1              @ we are going to 
ldr r1, =spc		@ print spaces
ldr r2, =len	
mov r7, $4
svc $0
ldr r3, [r6, #4]!	@ load next prime
bal printLoop           @ continue printing

newline:                @ we jump here if
mov r0, $1              @ we are going to
ldr r1, =nl             @ print a newline
mov r2, $1
mov r7, $4
svc $0
ldr r3, [r6, #4]!       @ load next prime
mov r4, $0              @ reset temporary counter
bal printLoop		@ continue printing

@@@@@@@@@@@@@@@@@@@@@@
@ print_num function @
@@@@@@@@@@@@@@@@@@@@@@

print_num:
	stmfd sp!, {r0-r9, lr}	@ push regs to stack
	mov r4, $0 		@ set division counter to zero
	mov r5, $1		@ set char counter to one

loop:				@ division routine
	cmp r3, $9		
	ble stackPush		@ if r3 <= 9, call stackPush
	sub r3, r3, $10		@ else, subtract 10 from r3
	add r4, r4, $1		@ add one to div. counter
	bal loop		@ repeat

stackPush:
	add r5, r5, $1		@ increment char counter
	orr r0, r3, $0x30	@ logical OR - add 48 to digit to get ascii code
	stmfd	sp!, {r0}	@ push onto stack
	cmp r4, $0		@ if the div. counter is zero ...
	beq printChars		@ call print function
	mov r3, r4		@ else, load div. count into r3
	mov r4, $0		@ reset div. counter
	bal loop		@ back to top of loop

printChars:
	mov r1, sp		@ use stack pointer to provide ascii code
	mov r0, $1		@ stdout is file descriptor 1
	mov r2, $1		@ length to print is 1
	mov r7, $4		@ write syscall
	svc $0			@ wake kernel
	subs r5, r5, $1		@ decrement string counter and set flag
	ble return		@ return if done
	ldmfd sp!, {r0}		@ pull next char from from stack 
	bal printChars		@ get next char
return:
	ldmfd sp!, {r0-r9, pc}	@ restore registers

exit:
mov r0, $1			@ print a newline
ldr r1, =nl
mov r2, $1
mov r7, $4
svc $0
mov r7, $1			@ exit
svc $0

.end
